const localData=localStorage.getItem('pc_pz')
const state =localData?localData.menu: {
    // 控制菜单打开和收起
    isCollapse: false,
    selectMenu: [],
    routerList:[]
}
const mutations = {
    // 控制菜单打开和收起
    collapseMenu(state) {
        state.isCollapse = !state.isCollapse
       
    },
    // 记录菜单meta数据
    addMenu(state, payload) {
        //对数据去重
        if (state.selectMenu.findIndex(item => item.path === payload.path) === -1) {
            state.selectMenu.push(payload)
        }
    },
    // 点击删除tag数据
    closeMenu(state, payload) {
        // 找到对应索引
       const index= state.selectMenu.findIndex(val => val.name === payload.name)
        // 通过索引删除数组指定元素
        state.selectMenu.splice(index,1)
    },
    //登陆页面权限数据存储
dynamicMenu(state, payload) {
    // 注意：views目录，而不是view
    const modules = import.meta.glob('../views/**/*.vue')
    function routerSet(routes) {
        routes.forEach(route => {
            if (route.children) {
                routerSet(route.children)
            } else {
                // 使用route.path构建组件路径
                const componentPath = `../views${route.path}/index.vue`
                // 从模块映射中获取异步组件函数
                route.component = modules[componentPath]
                // 如果没有找到，可能是路径问题，打印错误
                if (!route.component) {
                    console.error(`Component not found: ${componentPath}`)
                }
            }
        })
    }
    routerSet(payload)
    state.routerList = payload
}

}
export default {
    state,
    mutations
}